<?php

namespace app\home\controller;

use app\company\service\DataService;
use library\Controller;
use think\Db;
use app\common\controller\Email;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Sum;

/**
 * 支付类订单列表管理
 * Class User
 * @package app\worker\controller
 */
class PayOrder extends Controller
{
    /**
     * 绑定当前数据表
     * @var string
     */
    protected $table = 'body';

    /**
     * 资源订单列表管理
     * @auth true
     * @menu true
     */
    public function body_index()
    {
        $this->title = '资源订单列表管理';
        $query = $this->_query('body')
            ->alias('b')
            ->join('user u', 'u.body_id = b.id', 'left')
            ->join('order o', 'o.uid = u.id', 'left')
            ->join('order_detail od', 'od.order_id = o.id', 'left')
            ->join('plan p', 'p.id = od.plan_id', 'left')
            ->join('ware_house wh', 'wh.id = p.ware_house_id', 'left')
            ->where([
                'u.is_deleted' => 0,
                'b.is_deleted' => 0,
                'o.is_deleted' => 0,
                'od.is_deleted' => 0,
                'p.is_deleted' => 0,
                'wh.is_deleted' => 0,
                'u.status' => 1,
                'b.status' => 1,
                'p.status' => 1,
                'wh.status' => 1,
            ])
            ->like('b.body_name#body_name,wh.name#wh_name')
            ->field('sum(od.complete_count) as sum,sum(od.price*od.complete_count) as sum_price,sum(od.timeout_fine_price+od.goods_fine_price+od.other_fine_price) as fine_price,
            wh.name as wh_name,wh.id as ware_house_id,
            b.*')
            ->order('wh.id desc,b.create_at desc')
            ->group('wh.id,b.id')
            ->page();
    }

    protected function _body_index_page_filter(&$data) { 
        
        foreach($data as &$v){
            $v['sum'] = $v['sum'];//完成件数
            $v['sum_price'] = $v['sum_price'];//总金额
            $v['fine_price'] = $v['fine_price'];//扣罚金额
            $v['reality_price'] = $v['sum_price'] - $v['fine_price'];//应发金额
            $v['complete_pay_price'] = Db::name('pay_log')
                ->where('table', 'body')
                ->where('primary_key', $v['id'])
                ->where('ware_house_id', $v['ware_house_id'])
                ->sum('price');//已付金额
            $v['wait_pay_price'] = $v['reality_price'] - $v['complete_pay_price'];//待支付金额
        }
    }

    /**
     * 人员订单列表管理
     * @auth true
     * @menu true
     */
    public function user_index()
    {
        $this->title = '人员订单列表管理';
        $query = $this->_query('order')
            ->alias('o')
            ->join('user u', 'u.id = o.uid', 'left')
            ->join('order_detail od', 'od.order_id = o.id', 'left')
            ->join('plan p', 'p.id = od.plan_id', 'left')
            ->join('ware_house wh', 'wh.id = p.ware_house_id', 'left')
            ->where([
                'u.is_deleted' => 0,
                'o.is_deleted' => 0,
                'od.is_deleted' => 0,
                'p.is_deleted' => 0,
                'wh.is_deleted' => 0,
                'u.status' => 1,
                'p.status' => 1,
                'wh.status' => 1,
            ])
            ->like('u.name#name,wh.name#wh_name')
            ->field('sum(od.complete_count) as sum,sum(od.price*od.complete_count) as sum_price,sum(od.timeout_fine_price+od.goods_fine_price+od.other_fine_price) as fine_price,
                o.uid,o.timeout_fine_count,o.timeout_fine_price,o.goods_fine_price,o.other_fine_price,o.is_group,
                wh.name as wh_name,wh.id as ware_house_id,
                u.*')
            ->order('wh.id desc,u.create_at desc')
            ->group('wh.id,u.id')
            ->page();       
    }

    protected function _user_index_page_filter(&$data) {    
        foreach($data as &$v){
            $v['sum'] = $v['sum'];//完成件数
            $v['sum_price'] = $v['sum_price'];//总金额
            $v['fine_price'] = $v['fine_price'];//扣罚金额
            $v['reality_price'] = $v['sum_price'] - $v['fine_price'];//应发金额
            $v['complete_pay_price'] = Db::name('pay_log')
                ->where('table', 'user')
                ->where('primary_key', $v['id'])
                ->where('ware_house_id', $v['ware_house_id'])
                ->sum('price');//已付金额
            $v['wait_pay_price'] = $v['reality_price'] - $v['complete_pay_price'];//待支付金额
        }
    }

    /**
     * 确认打款
     * @auth true
     */
    public function confirm()
    {
        if(request()->isPost()){
            if(request()->post('price') > request()->post('wait_pay_price')){
                $this->error('打款金额不能超出待支付金额');
            }
            $data['evidence_time'] = request()->post('evidence_time');
            $data['evidence_image'] = request()->post('evidence_image');
            $data['invoice_type'] = request()->post('invoice_type');
            $data['price'] = request()->post('price');
            $data['table'] = request()->post('table');
            $data['primary_key'] = request()->post('id');
            $data['ware_house_id'] = request()->post('ware_house_id');
            $time = explode(' - ', request()->post('time'));
            $data['start_time'] = $time[0];
            $data['end_time'] = $time[1];
            Db::name('pay_log')->insert($data);
            $this->success('确认打款成功');
        }else{
            $this->now_time = date('Y-m-d');
            $this->fetch();
        }
    }

    /**
     * 批量打款
     * @auth true
     */
    public function some_confirm()
    {
        if(request()->isGet()){
            $this->now_time = date('Y-m-d');
            $this->fetch();
        }else{
            $post = request()->post();
            $ids = explode(',', $post['ids']);
            $time = explode(' - ', request()->post('time'));
            $data = [];
            foreach($ids as $v){
                $item = explode('-', $v);
                if($post['price'] > $item[2]){
                    $this->error('打款失败，所勾选的数据中存在打款金额超出待支付金额!');
                }
                $data[] = [
                    'evidence_time' => $post['evidence_time'],
                    'evidence_image' => $post['evidence_image'],
                    'invoice_type' => $post['invoice_type'],
                    'price' => $post['price'],
                    'table' => $post['table'],
                    'primary_key' => $item[0],
                    'ware_house_id' => $item[1],
                    'start_time' => $time[0],
                    'end_time' => $time[1],
                ];
            }
            Db::name('pay_log')->insertAll($data);
            $this->success('批量打款成功');
        }
    }

    /**
     * 支付类订单明细
     * @auth true
     */
    public function order_detail()
    {}

    /**
     * 支付记录
     * @auth true
     */
    public function pay_log_index()
    {}
}
